var BaseConst = require('../../../Base/server/controllers/Const'),
    publicFun = require('../../../Base/server/controllers/Function'),
    barcodeMasterController = require('../../../Barcode/server/controllers/BarcodeMaster.server.controller'),
    bizRecordController = require('../../../Barcode/server/controllers/BizRecord.server.controller'),
    rcvRecordController = require('../../../ProduceWork/server/controllers/RcvRecord.server.controller'),
    departmentController = require('../../../Barcode/server/controllers/Department.server.controller'),
    terminalController = require('../../../CBO/server/controllers/Terminal.server.controller'),
    ParameterFunction = require('../../../Common/server/controllers/Parameter.server.function');

// 服务器连接测试接口
exports.connectTest = function(req, res) {
    ParameterFunction.getParameterValue('GatewayServer', function (err, val) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
            return;
        }
        res.json({
            Data: {
                GatewayAddress: val,
                SocketPort: BaseConst.SocketPort
            },
            Error: null
        });       
    });
};

// 获取系统当前时间
exports.getSystemTime = function (req, res) {
    var format = req.body.Format,
        terminalUniqueID = req.body.TerminalUniqueID;

    terminalController.updateByValue(terminalUniqueID, { LastRequestDate: new Date() }, function () {
        var cTime = new Date(),
            result;
        if (format === 'ISO') {
            result = cTime.toISOString();
        } else if (format === 'GMT' || format === 'UTC') {
            result = cTime.toUTCString();
        } else if (format === 'String') {
            result = cTime.toString();
        } else if (format === 'STAMP') {
            result = cTime.valueOf();
        } else {
            var temp = cTime.toString(),
                tSplits = temp.split(' ');
            result = tSplits[0] + ' ' + tSplits[1] + ' ' + tSplits[2] + ' ' + tSplits[4] + ' ' + tSplits[3];
        }
        res.json({
            Data: result,
            Error: null
        });
    });
};

//生产批次产量分布看板
exports.findAllBarcodeMasterByLotNum = function(req, res) {
    var parameter = req.body.Parameter;
    var MOLotNumber = parameter.MOLotNumber;
    barcodeMasterController.findAllBarcodeMaster(MOLotNumber, function(err, result) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            res.json({
                Data: result,
                Error: null
            });
        }
    });
};
//质量预警(按时间分组)
exports.getBarcodeMasterByDate = function(req, res) {
    bizRecordController.qualityWaring(function(err, result) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            res.json({
                Data: result,
                Error: null
            });
        }

    });
};

//查询完工日报
exports.queryCompletionDaily = function(req, res) {
    rcvRecordController.queryRcvRecords(function(err, result) {
        if (err) {
            res.json({
                Data: null,
                Error: err

            });
        } else {
            res.json({
                Data: result,
                Error: null
            });
        }
    });
};

//查询部门信息
exports.findDepartmentByCodeOrName = function(req, res) {
    var parameter = req.body.Parameter;
    var codeOrName = parameter.DepartmentData;
    departmentController.findAllDepartmentByName(codeOrName, function(err, result) {
        console.log(result);
        if (err) {
            res.json({
                Data: null,
                Error: err

            });
        } else {
            if(result==null){
                 departmentController.findAllDepartmentByCode(codeOrName, function(err, result) {
                 console.log(result);
                    if (err) {
                        res.json({
                            Data: null,
                            Error: err
                        });
                    }
                    else{
                        if (result == null) {
                            var newErr = new Error();
                            newErr.leval = 9;
                            newErr.title = '查询部门为空';
                            newErr.message = '未找到对应的部门！';
                            res.json({
                                Data: null,
                                Error: newErr
                            });
                        } else {
                            res.json({
                                Data: result,
                                Error: null
                            });
                        } 
                    }

                 });   
            }
            else{
                res.json({
                    Data: result,
                    Error: null
                }); 
            }
            
        }
    });
};
